package com.qianxx.component.activity.effects.scroll_panel;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;
import com.qianxx.viewlibrary.effects.scrollpanel.ScrollPanel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 仿饿了么商家主页
 */
public class ScrollPanelActivity extends BaseActivity {
    //第1层的控件
    @BindView(R.id.rl_discounts)
    RelativeLayout rl_discounts;
    @BindView(R.id.ll_announcement)
    LinearLayout llAnnouncement;
    @BindView(R.id.tv_down)
    TextView tvDown;
    @BindView(R.id.rcv_discounts)
    RecyclerView rcvDiscounts;

    //第2层的控件
    @BindView(R.id.tv_search)
    TextView tvSearch;

    //第3层的控件
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.iv_like)
    ImageView ivLike;
    @BindView(R.id.iv_title_bg)
    ImageView ivTitleBg;
    @BindView(R.id.iv_back)
    ImageView ivBack;

    //第4层的控件
    @BindView(R.id.sp_menu)
    public ScrollPanel sp_menu;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_up)
    TextView tvUp;
    @BindView(R.id.view_line)
    View viewLine;

    //第5层的控件
    @BindView(R.id.rl_settlement)
    RelativeLayout rlSettlement;

    ViewPagerAdapter adapter;
    DiscountsAdapter discountsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll_panel);
        ButterKnife.bind(this);

        //优化信息
        discountsAdapter = new DiscountsAdapter(this, initData());
        rcvDiscounts.setLayoutManager(new LinearLayoutManager(this));
        rcvDiscounts.setAdapter(discountsAdapter);

        //滚动控件
        sp_menu.setOnScrollListener(new ScrollPanel.OnScrollListener() {

            @Override
            public void onScrollTo(int x, int y) {
                /********************** sp_menu向上滑动处理 ************************/

                //“优惠信息面板”跟随sp_menu上移
                int left = 0;
                int right = left + rl_discounts.getWidth();
                int top = y - rl_discounts.getHeight() / 2 > 0 ? 0 : y - rl_discounts.getHeight() / 2;
                int bottom = top + rl_discounts.getHeight();
                rl_discounts.layout(left, top, right, bottom);

                //获取“优惠信息面板”当前上滑动的位置
                float a = Math.abs(rl_discounts.getTop());
                //获取“优惠信息面板”可上滑动的总距离
                float b = (sp_menu.getMiddleY() - sp_menu.getTopY());
                //计算滑动的距离所占比
                float per = a / b;

                //输入框透明度渐变
                tvSearch.setAlpha(per);
                //标题栏背景色渐变
                ivTitleBg.setAlpha(1 - per);
                //设置返回键的图标
                ivBack.setBackgroundResource(per <= 0
                        ? R.drawable.fanhui : R.drawable.hei);

                //店铺图标缩放
                ivIcon.setScaleX(1 - per);
                ivIcon.setScaleY(1 - per);
                //收藏图标缩放
                ivLike.setScaleX(1 - per);
                ivLike.setScaleY(1 - per);

                //设置状态栏是否是黑色文字、图标
                statusBarLightMode(ScrollPanelActivity.this, per == 1
                        ? true : false);

                /********************** sp_menu向下滑动处理 ************************/

                //结算栏随sp_menu下滑
                int t = y + (sp_menu.getMiddleY() - rlSettlement.getHeight());
                t = t < sp_menu.getHeight() - rlSettlement.getHeight()
                        ? sp_menu.getHeight() - rlSettlement.getHeight() : t;
                rlSettlement.layout(0, t, rlSettlement.getWidth(), t + rlSettlement.getHeight());

                //获取sp_menu当前下滑动的位置
                float c = Math.abs(y - sp_menu.getMiddleY() < 0 ? 0 : y - sp_menu.getMiddleY());
                //获取sp_menu可下滑动的总距离
                float d = Math.abs(sp_menu.getMiddleY() - sp_menu.getBottomY());
                //计算滑动的距离所占比
                float p = c / d;

                //公告透明度渐变
                llAnnouncement.setAlpha(1 - p);
                //优惠信息列表透明度渐变
                rcvDiscounts.setAlpha(p);
                //向上按钮透明度渐变
                tvUp.setAlpha(p);
                //设置向上按钮是否可见
                tvUp.setVisibility(p == 1 ? View.VISIBLE : p == 0 ? View.GONE : View.VISIBLE);
                //TabLayout透明度渐变
                tabLayout.setAlpha(1 - p);
                //分割线渐变
                viewLine.setAlpha(1 - p);
                //viewPage透明度渐变
                viewPager.setAlpha(1 - p);
            }
        });

        //TabLayout
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    private List<String> initData() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("超级会员领6元无槛红包");
        }
        return list;
    }


    @OnClick({R.id.tv_down, R.id.tv_up})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_down:
            case R.id.tv_up:
                if (!sp_menu.isBottom()) {
                    sp_menu.smoothScrollTo(0, -sp_menu.getBottomY());
                } else {
                    sp_menu.smoothScrollTo(0, -sp_menu.getMiddleY());
                }
                break;
        }
    }

    /**
     * 不显示自定义标题栏
     *
     * @return
     */
    @Override
    public boolean setEnableTitleBar() {
        return false;
    }

    /**
     * 不显示系统状态栏
     *
     * @return
     */
    @Override
    public boolean setEnabledStatusBar() {
        return false;
    }
}
